package org.zhangchao.poi.excel.common;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.zhangchao.poi.excel.common.annotation.DateResources;
import org.zhangchao.poi.excel.common.annotation.ExcelResources;

/**
 * 用来存储Excel标题的对象，通过该对象可以获取列标题和方法的对应关系
 * @author zhangchao
 */
public class ExcelHeader implements Comparable<ExcelHeader> {

	/** excel列标题 */
	private String columnTitle;
	
	/** 字段名 */
	private String fieldName;

	/** 标题的顺序序号 */
	private int order;
	
	/** Date类型的字符串格式 */
	private String datePattern;
	
	public ExcelHeader() {
	}

	/**
	 * 
	 * @param columnTitle
	 * @param fieldName
	 * @param order
	 */
	public ExcelHeader(String columnTitle, String fieldName, int order) {
		super();
		this.columnTitle = columnTitle;
		this.fieldName = fieldName;
		this.order = order;
	}
	
	/**
	 * 
	 * @param columnTitle
	 * @param fieldName
	 * @param order
	 * @param datePattern
	 */
	public ExcelHeader(String columnTitle, String fieldName, int order,
			String datePattern) {
		super();
		this.columnTitle = columnTitle;
		this.fieldName = fieldName;
		this.order = order;
		this.datePattern = datePattern;
	}

	/**
	 * 解析实体类
	 * @param clazz
	 * @return
	 */
	public static List<ExcelHeader> buildHeaderList(Class<?> clazz) {
		List<ExcelHeader> headerList = new ArrayList<ExcelHeader>();
		Field[] fields = clazz.getDeclaredFields();
		//首先解析field
		for (Field field : fields) {
			String fieldName = field.getName();
			//解析ExcelResources注解
			if (field.isAnnotationPresent(ExcelResources.class)) {
				ExcelResources er = field.getAnnotation(ExcelResources.class);
				String pattern = null;
				//解析DateResources注解
				if(field.isAnnotationPresent(DateResources.class)) {
					DateResources dr = field.getAnnotation(DateResources.class);
					pattern = dr.pattern();
				}
				ExcelHeader eh = new ExcelHeader(er.columnTitle(), fieldName, er.order(), pattern);
				headerList.add(eh);
			}
		}
		//解析方法
		if(headerList.size() == 0) {
			Method[] methods = clazz.getDeclaredMethods();
			for (Method method : methods) {
				String methodName = method.getName();
				if (methodName.startsWith("set") || methodName.startsWith("get")) {
					//解析ExcelResources注解
					if (method.isAnnotationPresent(ExcelResources.class)) {
						ExcelResources er = method.getAnnotation(ExcelResources.class);
						String fieldName = methodName.substring(3);
						fieldName = fieldName.substring(0,1).toLowerCase()+fieldName.substring(1);
						String pattern = null;
						//解析DateResources注解
						if(method.isAnnotationPresent(DateResources.class)) {
							DateResources dr = method.getAnnotation(DateResources.class);
							pattern = dr.pattern();
						}
						ExcelHeader eh = new ExcelHeader(er.columnTitle(), fieldName, er.order(), pattern);
						headerList.add(eh);
					}
				}
			}
		}
		//排序
		Collections.sort(headerList);
		return headerList;
	}
	
	public int compareTo(ExcelHeader excelHeader) {
		return order > excelHeader.order ? 1 : (order < excelHeader.order ? -1 : 0);
	}

	public String getColumnTitle() {
		return columnTitle;
	}

	public void setColumnTitle(String columnTitle) {
		this.columnTitle = columnTitle;
	}

	public String getFieldName() {
		return fieldName;
	}

	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}

	public int getOrder() {
		return order;
	}

	public void setOrder(int order) {
		this.order = order;
	}

	public String getDatePattern() {
		return datePattern;
	}

	public void setDatePattern(String datePattern) {
		this.datePattern = datePattern;
	}

}
